<?php
class plugin_lib {

	/**
	 * 获取所有插件
	 *
	 * @return unknown
	 */
	static function getAllPlugin(){
		$xdata = array();
		if(preg_match('/lan7\.net/i',HTTP_HOST)){
			$xdata = array();
			$q = $GLOBALS['db']->query("select * from ".tn('plugin')." order by Id desc");
			while($row = $GLOBALS['db']->fetch_array($q)){
				$xdata[$row['p_code']] = $row;
			}
		}else{
			$str = file_get_contents(ORA_UPDATE."/update/pluginXml");
			if(sizeof($str)>0){
				$xobj = @simplexml_load_string($str);
				$count = sizeof($xobj->plugin);
				$xdata = array();
				for($j=0;$j<$count;$j++){
					$tmp = array();
					foreach($xobj->plugin[$j]->attributes() as $a => $b) {
						$tmp[(string)$a] = (string)$b;
					}
					$xdata[$tmp['p_code']] = $tmp;
				}
			}
		}
		return $xdata;
	}

	/**
	 * 安装插件
	 *
	 * @param unknown_type $p_code
	 * @param unknown_type $rs
	 * @return unknown
	 */
	static function install($p_code,$rs=false){
		if(@include(PLUGIN_PATH.$p_code."/index.php")){
			$tmp = new $p_code();
			return $tmp->install($rs);
		}
		return false;
	}

	/**
	 * 卸载插件
	 *
	 * @param unknown_type $p_code
	 * @return unknown
	 */
	static function uninstall($p_code){
		if(@include(PLUGIN_PATH.$p_code."/index.php")){
			$tmp = new $p_code();
			if($tmp->uninstall()){
				io::removeDir(PLUGIN_PATH.$p_code."/");
				return true;
			}
		}
		return false;
	}

	/**
	 * 验证是否合法插件
	 *
	 * @abstract 验证插件类是否实现接口所定义的方法
	 * @param String $code
	 * @param String $path
	 */
	static function validate($code,$path){
		$rule = array('getName','getVer','getAuthor','getReqVer','admin','index','install','uninstall','getCode');
		if(unpackZip(PLUGIN_PATH.$code."/",UPLOAD_PATH.$path)){
			$s = io::read(PLUGIN_PATH.$code."/index.php");
			foreach($rule as $k => $v){
				if(!preg_match('/function\s+('.$v.')/i',$s)){
					io::removeDir(PLUGIN_PATH.$code."/");
					return false;
				}
			}
			$s = null;
			return true;
		}
		return false;
	}

	/**
	 * 添加至站点导航
	 *
	 * @param unknown_type $code
	 * @param unknown_type $title
	 * @param unknown_type $doAdd
	 * @return unknown
	 */
	static function addNav($code,$title,$doAdd=true){
		if(!preg_match('/^[a-zA-Z]+\w*?$/i',$code)){
			return false;
		}
		$title =clear_html($title);
		$sql = null;
		if($doAdd){
			$sql = "insert into ".tn('nav')."(nav_title,nav_url,nav_target,nav_code)";
			$sql.= " values('$title','0',0,'loader-index-pn-$code')";
		}else{
			$sql = "delete from ".tn('nav')." where nav_code = 'loader-index-pn-$code' and nav_title = '$title'";
		}
		if($GLOBALS['db']->execute($sql)){
			return true;
		}
		return false;
	}

	/**
	 * 删除插件导航
	 *
	 * @param unknown_type $code
	 * @param unknown_type $title
	 * @return unknown
	 */
	static function removeNav($code,$title){
		if(!preg_match('/^[a-zA-Z]+\w*?$/i',$code)){
			return false;
		}
		$title =clear_html($title);
		$sql = "delete from ".tn('nav')." where nav_code = 'loader-index-pn-$code' and nav_title = '$title'";
		if($GLOBALS['db']->execute($sql)){
			return true;
		}
		return false;
	}

	/**
	 * 添加插件管理链接至后台管理导航
	 *
	 * @param unknown_type $code
	 * @param unknown_type $title
	 * @param unknown_type $doAdd
	 * @return unknown
	 */
	static function addQuickStart($code,$title,$doAdd=true){
		if(!preg_match('/^[a-zA-Z]+\w*?$/i',$code)){
			return false;
		}
		$title =clear_html($title);
		$a = '<li><a href="{:parse_uri("admin_plugin-mgr-pn-'.$code.'|true")}" target="mainFrame">'.$title.'</a></li>';
		$leftHtmlFile = PUBLIC_PATH."admin/left.html";
		$s = io::read($leftHtmlFile);
		$s = str_replace($a,'',$s);
		if($doAdd){
			$s = preg_replace('/\<\!\-\-\s+PLUGINQUICKSTART\s+\-\-\>/i',$a."<!-- PLUGINQUICKSTART -->",$s);
		}else{
			$s = str_replace($a,'',$s);
		}
		if(io::write($leftHtmlFile,$s)){
			return true;
		}
		return false;
	}

	/**
	 * 删除快捷链接
	 *
	 * @param unknown_type $code
	 * @param unknown_type $title
	 * @return unknown
	 */
	static function removeQuickStart($code,$title){
		if(!preg_match('/^[a-zA-Z]+\w*?$/i',$code)){
			return false;
		}
		$title =clear_html($title);
		$a = '<li><a href="{:parse_uri("admin_plugin-mgr-pn-'.$code.'|true")}" target="mainFrame">'.$title.'</a></li>';
		$leftHtmlFile = PUBLIC_PATH."admin/left.html";
		$s = io::read($leftHtmlFile);
		$s = str_replace($a,'',$s);
		if(io::write($leftHtmlFile,$s)){
			return true;
		}
		return false;
	}

	/**
	 * 插件管理
	 *
	 * @param unknown_type $p_code
	 */
	static function admin($p_code){
		if(@include(PLUGIN_PATH.$p_code."/index.php")){
			$tmp = new $p_code();
			$tmp->admin();
		}
	}
}
?>